USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_list_insert') BEGIN
	DROP  FUNCTION  tsql.fn_list_insert
	PRINT '<<< DROP FUNCTION tsql.fn_list_insert - Completed with SUCCESS >>>'
END
GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/*================================================================================================
Name:			tsql.fn_list_insert
Type:			User defined function 
Owner:					
Description:	Sorts the elements in a list ASCENDIND/DESCENDING   
Parameters:		@list = list of numeric values comma delimited
				@value = value to be inserted into the list
				@position_no_no = default null, add the new value at the end 
Dependens on:  	fn_split
Usage:			SELECT [tsql].[fn_list_insert] ('7,2,12,23,3,4,5','36',DEFAULT)
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=fn_list_insert&referringTitle=Documentation		
Version:		20120905
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
--------------------------------------------------------------------------------------------------
History:		09/05/2012 - Adrian E Dudau - Function created
================================================================================================*/

CREATE FUNCTION [tsql].[fn_list_insert] (@list NVARCHAR(MAX),@value NVARCHAR(MAX),@position_no BIGINT = NULL)
RETURNS NVARCHAR(MAX) 
AS

BEGIN /*FUNCTION*/
	DECLARE @Result NVARCHAR(MAX)
	DECLARE @item_value NVARCHAR(MAX)

	SET @result = ''

	IF @position_no IS NULL OR @position_no >= tsql.fn_count_val(@list,0) BEGIN 
		SET @result = @list + ',' + ISNULL(@value,'NULL')
	END
	ELSE BEGIN 
		DECLARE curs CURSOR FOR 
		SELECT item_value 
		FROM [tsql].[fn_split](@list,',')
		WHERE id < @position_no 
		UNION ALL 
		SELECT 'item_value' = ISNULL(@value,'NULL') 
		UNION ALL 
		SELECT item_value 
		FROM [tsql].[fn_split](@list,',')
		WHERE id >= @position_no 
		 
		OPEN curs
		FETCH NEXT FROM curs INTO @item_value

		WHILE @@FETCH_STATUS = 0 BEGIN
		SELECT  @result = @result + CASE WHEN @result = '' THEN '' ELSE ',' END + @item_value
		FETCH NEXT FROM curs INTO @item_value
		END

		CLOSE curs
		DEALLOCATE curs
	END	

		RETURN @Result 
		
END /*FUNCTION*/
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------
IF EXISTS(SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'fn_list_insert') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_list_insert - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_list_insert - Completed with ERROR >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------


